package org.softmed.rest.editor.comps.browser;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;

import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.comps.ApplicationEditor;
import org.softmed.rest.editor.comps.HandlerEditor;
import org.softmed.rest.editor.comps.HttpMethodEditor;
import org.softmed.rest.editor.comps.MimeTypeHandlerEditor;
import org.softmed.rest.editor.comps.ModuleEditor;
import org.softmed.rest.editor.comps.ParameterEditor;
import org.softmed.rest.editor.comps.ResourceEditor;
import org.softmed.rest.editor.comps.SimpleEditor;
import org.softmed.rest.editor.comps.full.ApplicationEditorFull;
import org.softmed.rest.editor.comps.full.HandlerEditorFull;
import org.softmed.rest.editor.comps.full.HttpMethodEditorFull;
import org.softmed.rest.editor.comps.full.MimeTypeHandlerEditorFull;
import org.softmed.rest.editor.comps.full.ModuleEditorFull;
import org.softmed.rest.editor.comps.full.ParameterEditorFull;
import org.softmed.rest.editor.comps.full.ResourceEditorFull;
import org.softmed.swing.IconManager;

public class ClassSelector extends JPanel implements ActionListener {

	JRadioButton application = new JRadioButton("Applications", true);
	JRadioButton module = new JRadioButton("Modules", false);
	JRadioButton resource = new JRadioButton("Resources", false);
	JRadioButton httpMethod = new JRadioButton("Http Methods", false);
	JRadioButton mimeType = new JRadioButton("Mime Type Handlers", false);
	JRadioButton handler = new JRadioButton("Handlers", false);
	JRadioButton parameter = new JRadioButton("Parameters", false);

	JLabel viewing = new JLabel();
	JButton create = new JButton(IconManager.add);
	JButton refresh = new JButton(IconManager.refresh);

	URIProvider rootAdminURIProvider;
	PagedListPanel pagedListPanel;

	ButtonGroup group = new ButtonGroup();
	Object source = application;
	SelectionListener listener;

	String uriParams = "?detailed&size=30";

	// String uriParams = "?detailed";

	public ClassSelector(URIProvider rootAdminURIProvider,
			PagedListPanel pagedListPanel) {
		this.pagedListPanel = pagedListPanel;
		this.rootAdminURIProvider = rootAdminURIProvider;
		SwingUtil.setSize(this, EditorUtil.restBrowserWidth, 100);
		MigLayout layout = new MigLayout("left,insets 0");
		setLayout(layout);

		EditorUtil.configButton(create);
		EditorUtil.configButton(refresh);

		create.setToolTipText("Create new object of the selected category");
		refresh.setToolTipText("Refresh current page");

		viewing.setFont(new Font("Verdana", Font.BOLD, 14));

		add(application, "h 20!");
		add(httpMethod, "h 20!");
		add(parameter, "h 20!,wrap");

		add(module, "h 20!");
		add(mimeType, "h 20!,wrap");

		add(resource, "h 20!");
		add(handler, "h 20!, wrap");
		add(new JSeparator(SwingConstants.HORIZONTAL), "span 3,w "
				+ EditorUtil.smallWidth + "!,wrap");
		add(viewing, "w 190!, span 2, split 2");
		// add(create, "w 20!, h 20!");
		// add(refresh, "w 20!,h 20!");

		// add(new JSeparator(SwingConstants.HORIZONTAL), "span 3,w "
		// + EditorUtil.smallWidth + "!");

		application.addActionListener(this);
		module.addActionListener(this);
		resource.addActionListener(this);
		httpMethod.addActionListener(this);
		mimeType.addActionListener(this);
		handler.addActionListener(this);
		parameter.addActionListener(this);

		create.addActionListener(this);
		refresh.addActionListener(this);

		group.add(application);
		group.add(module);
		group.add(resource);
		group.add(httpMethod);
		group.add(mimeType);
		group.add(handler);
		group.add(parameter);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			Object tsource = e.getSource();
			String uri = rootAdminURIProvider.getURI();
			if (tsource == application) {
				showApplications();
				// return;
			}

			if (tsource == module) {
				pagedListPanel.setBaseClassURI(uri + "/" + "modules"
						+ uriParams);
				viewing.setText("Modules");
				// return;
			}

			if (tsource == resource) {
				pagedListPanel.setBaseClassURI(uri + "/" + "resources"
						+ uriParams);
				viewing.setText("Resources");
				// return;
			}

			if (tsource == httpMethod) {
				pagedListPanel.setBaseClassURI(uri + "/" + "http-methods"
						+ uriParams);
				viewing.setText("Http Methods");
				// return;
			}

			if (tsource == mimeType) {
				pagedListPanel.setBaseClassURI(uri + "/" + "mime-type-handlers"
						+ uriParams);
				viewing.setText("Mime Type Handlers");
				// return;
			}

			if (tsource == handler) {
				pagedListPanel.setBaseClassURI(uri + "/" + "handlers"
						+ uriParams);
				viewing.setText("Handlers");
				// return;
			}

			if (tsource == parameter) {
				pagedListPanel.setBaseClassURI(uri + "/" + "parameters"
						+ uriParams);
				viewing.setText("Parameters");
				// return;
			}

			if (tsource == create) {
				createNewObject();
			}

			if (tsource == refresh) {
				pagedListPanel.refresh();
			}

			if (tsource != create && tsource != refresh)
				source = tsource;

		} catch (Throwable t) {
			EditorUtil.hideUpdatePanel();
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	public void createNewObject() {
		try {
			SimpleEditor editor = null;
			// source = e.getSource();
			// String uri = rootAdminURIProvider.getURI();
			if (source == application) {
				editor = new ApplicationEditorFull(rootAdminURIProvider);
			}

			if (source == module) {
				editor = new ModuleEditorFull(rootAdminURIProvider);
			}

			if (source == resource) {
				editor = new ResourceEditorFull(rootAdminURIProvider);
			}

			if (source == httpMethod) {
				editor = new HttpMethodEditorFull(rootAdminURIProvider);
			}

			if (source == mimeType) {
				editor = new MimeTypeHandlerEditorFull(rootAdminURIProvider);
			}

			if (source == handler) {
				editor = new HandlerEditorFull(rootAdminURIProvider);
			}

			if (source == parameter) {
				editor = new ParameterEditorFull(rootAdminURIProvider);
			}

			EditorUtil.showUpdatePanel();

			editor.createNew();

			listener.selected(editor.getEntity());

			EditorUtil.hideUpdatePanel();

		} catch (Throwable t) {
			EditorUtil.hideUpdatePanel();
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public void showApplications() throws Throwable {
		// try {
		String uri = rootAdminURIProvider.getURI();
		pagedListPanel.setBaseClassURI(uri + "/" + "applications" + uriParams);
		viewing.setText("Applications");
		// } catch (Throwable t) {
		// t.printStackTrace();
		// }
	}

	public void setSelectionListener(SelectionListener listener) {
		this.listener = listener;
	}

	public JButton getCreate() {
		return create;
	}

	public JButton getRefresh() {
		return refresh;
	}

	public void disableButtons() {
		setEnabledStateToButtons(false);

	}

	public void enableButtons() {
		setEnabledStateToButtons(true);

	}

	private void setEnabledStateToButtons(boolean state) {
		application.setEnabled(state);
		module.setEnabled(state);
		resource.setEnabled(state);
		httpMethod.setEnabled(state);
		mimeType.setEnabled(state);
		handler.setEnabled(state);
		parameter.setEnabled(state);

		create.setEnabled(state);
		refresh.setEnabled(state);
	}

}
